/**************************************************
|	File:		MapImage.h
|	Author:		Benjamin Budian
|	Course:		SGD
|	Purpose:	Handles storing and rendering of background images
**************************************************/

#include "MapImage.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../TinyXML/tinyxml.h"
#include "GameplayState.h"
#include "EntityManager.h"
#include "Object.h"

#include <cassert>



CMapImage::CMapImage(void){
	/*tile_list[INSIDE_CAVE_TOP_LEFT] = 36;
	tile_list[INSIDE_CAVE_TOP_WALL1] = 65;
	tile_list[INSIDE_CAVE_TOP_RIGHT] = 35;
	tile_list[INSIDE_CAVE_LEFT_TOP_WALL1] = 68;
	tile_list[INSIDE_CAVE_TOP_WALL2] = 97;
	tile_list[INSIDE_CAVE_RIGHT_TOP_WALL1] = 67;
	tile_list[INSIDE_CAVE_LEFT_TOP_WALL2] = 100;
	tile_list[INSIDE_CAVE_TOP_WALL3] = 129;
	tile_list[INSIDE_CAVE_RIGHT_TOP_WALL2] = 99;
	tile_list[INSIDE_CAVE_LEFT_WALL] = 34;
	tile_list[INSIDE_CAVE_RIGHT_WALL] = 32;
	tile_list[INSIDE_CAVE_BOTTOM_LEFT] = 4;
	tile_list[INSIDE_CAVE_BOTTOM] = 1;
	tile_list[INSIDE_CAVE_BOTTOM_RIGHT] = 3;
	tile_list[CAVE_FLOOR] = 38;*/

}

CMapImage::~CMapImage(void)
{
	if (m_nImageID != -1){
		CSGD_TextureManager::GetInstance()->UnloadTexture(m_nImageID);
		m_nImageID = -1;
	}
}

void CMapImage::Initialize(/*Input Rows and Column desired here*/ void){

	LoadMap();

	for (int col = 0; col < MAP_COL; col++){
		for (int row = 0; row < MAP_ROW; row++){
			//Top left corner
			if (row == 0 && col == 0){
				m_nGameMap[row][col] = tile_list[INSIDE_CAVE_TOP_LEFT];
			}
			//Top right corner
			else if (col == 0 && row == MAP_ROW - 1){
				m_nGameMap[row][col] = tile_list[INSIDE_CAVE_TOP_RIGHT];
			}
			//Row 1
			else if (col == 0){
				m_nGameMap[row][col] = tile_list[INSIDE_CAVE_TOP_WALL1];
			}
			//Row 2 first cell
			else if (col == 1 && row == 0){
				m_nGameMap[row][col] = tile_list[INSIDE_CAVE_LEFT_TOP_WALL1];
			}
			//Row 2 last cell
			else if (col == 1 && row == MAP_ROW - 1){
				m_nGameMap[row][col] = tile_list[INSIDE_CAVE_RIGHT_TOP_WALL1];
			}
			//Row 2
			else if (col == 1){
				m_nGameMap[row][col] = tile_list[INSIDE_CAVE_TOP_WALL2];
			}
			//Row 3 first cell
			else if (col == 2 && row == 0){
				m_nGameMap[row][col] = tile_list[INSIDE_CAVE_LEFT_TOP_WALL2];
			}
			//Row 3 last cell
			else if (col == 2 && row == MAP_ROW - 1){
				m_nGameMap[row][col] = tile_list[INSIDE_CAVE_RIGHT_TOP_WALL2];
			}
			//Row 3
			else if (col == 2){
				m_nGameMap[row][col] = tile_list[INSIDE_CAVE_TOP_WALL3];
			}
			//Bottom left corner
			else if (col == MAP_COL - 1 && row == 0){
				m_nGameMap[row][col] = tile_list[INSIDE_CAVE_BOTTOM_LEFT];
			}
			//Bottom right corner
			else if (col == MAP_COL - 1 && row == MAP_ROW - 1){
				m_nGameMap[row][col] = tile_list[INSIDE_CAVE_BOTTOM_RIGHT];
			}
			//Bottom row
			else if (col == MAP_ROW - 1){
				m_nGameMap[row][col] = tile_list[INSIDE_CAVE_BOTTOM];
			}
			else if (row == 0){
				m_nGameMap[row][col] = tile_list[INSIDE_CAVE_LEFT_WALL];
			}
			else if (row == MAP_ROW - 1){
				m_nGameMap[row][col] = tile_list[INSIDE_CAVE_RIGHT_WALL];
			}
			// All other cells;
			else
				m_nGameMap[row][col] = tile_list[CAVE_FLOOR];

			m_nGameBackground[row][col] = tile_list[CAVE_FLOOR];
		}

		LoadMap();

		m_nImageID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/images/bb_terrain_atlas.png"));

		m_nTileWidth = 32;
		m_nTileHeight = 32;
	}
}

void CMapImage::Shutdown(void){
	if (m_nImageID != -1){
		CSGD_TextureManager::GetInstance()->UnloadTexture(m_nImageID);
		m_nImageID = -1;
	}
}


void CMapImage::Render(void){
	for (int row = 0; row < MAP_ROW; row++){
		for (int col = 0; col < MAP_COL; col++){

			assert(m_nGameBackground[row][col] != -1 && "CMapImage::Render - cell was not initialized");

			RECT rCell = CellAlgorithm(m_nGameBackground[row][col]);

			CSGD_TextureManager::GetInstance()->Draw(m_nImageID, m_nTileWidth * row, m_nTileHeight * col, 1.0f, 1.0f, &rCell);
		}
	}

	for (int row = 0; row < MAP_ROW; row++){
		for (int col = 0; col < MAP_COL; col++){

			assert(m_nGameMap[row][col] != -1 && "CMapImage::Render - cell was not initialized");

			RECT rCell = CellAlgorithm(m_nGameMap[row][col]);

			CSGD_TextureManager::GetInstance()->Draw(m_nImageID, m_nTileWidth * row, m_nTileHeight * col, 1.0f, 1.0f, &rCell);
		}
	}
}

RECT CMapImage::CellAlgorithm(int id) const {
	RECT rCell = {};

	rCell.left = (id % MAP_COL) * m_nTileWidth;
	rCell.top = (id / MAP_COL) * m_nTileHeight;

	rCell.right = rCell.left + m_nTileWidth;
	rCell.bottom = rCell.top + m_nTileHeight;

	return rCell;
}

void CMapImage::LoadMap(){
	TiXmlDocument doc;

	//Load file - if none available return from function
	if (doc.LoadFile("world.xml") == false)
		return;

	//Get Root element
	TiXmlElement* root = doc.RootElement();

	//if root element is null return from function
	if (root == nullptr)
		return;

	//Load values to array
	TiXmlNode* map_values = root->FirstChild();

	TiXmlElement* map = map_values->FirstChildElement();

	for (int i = 0; i < TOTAL_TILES; i++){
		map->Attribute("value", &tile_list[i]);

		map = map->NextSiblingElement();
	}

	//Load Game Objects
	int number_objects;

	map->Attribute("value", &number_objects);
	map = map->NextSiblingElement();

	int row, col, id;
	for (int i = 0; i < number_objects; i++)
	{
		map->Attribute("row", &row);
		map->Attribute("col", &col);
		map->Attribute("value", &id);

		//Put Game Objects into Entity Manager
		/*m_nGameMap[row][col] = id;*/
		Object* object = CreateObject(row, col, id);
		CGameplayState::GetInstance()->GetEntityManager()->AddEntity(object, 3);

		map = map->NextSiblingElement();

	}
}

Object* CMapImage::CreateObject(int row, int column, int id){

	Object* object = nullptr;

	switch (id){
	case TREE_1:
		//////////////////////////////////
		// OBJECT TEST
		object = new Object(); // = new Tree();

		object->Initialize(row, column, _T("resource/images/tree_1.png"), _T("A basic tree...thing"));
		object->SetWidth(96);
		object->SetHeight(160);
		//////////////////////////////////

		break;

	case TREE_2:
		//////////////////////////////////
		// OBJECT TEST
		object = new Object(); // = new Tree();

		object->Initialize(row, column, _T("resource/images/tree_2.png"), _T("A basic tree...thing"));
		object->SetWidth(96);
		object->SetHeight(128);
		//////////////////////////////////
		break;

	case TREE_STUMP:
		//////////////////////////////////
		// OBJECT TEST
		object = new Object(); // = new TreeStump();

		object->Initialize(row, column, _T("resource/images/tree_stump.png"), _T("A basic tree...thing"));
		object->SetWidth(64);
		object->SetHeight(64);
		//////////////////////////////////
		break;

	case STONE_FACE:
		//////////////////////////////////
		// OBJECT TEST
		object = new Object(); // = new StoneFace();

		object->Initialize(row, column, _T("resource/images/stone_face.png"), _T("A basic tree...thing"));
		object->SetWidth(64);
		object->SetHeight(96);
		//////////////////////////////////
		break;
	}

	return object;
}

//void CMapImage::LoadTileset(){}
//
//void CMapImage::AddItem(int nRow, int nCol, int nItemID){}





